import numpy as np
data=np.load('data_for_MPR.npz')
# mask data
data_mask=data['data_mask']
# static data
data_static=data['data_static']
# forcing data
data_forcing=data['data_forcing']
# calibration data
data_cali = data['data_cali']



init_x=-129.98622161209474
init_y=53.988748575583244
end_x = init_x+0.08983152841195215*778
end_y = init_y-0.08983152841195215*377

#%% mask data

import netCDF4 as nc
import numpy as np

fn = './data_mask.nc'
ds = nc.Dataset(fn, 'w', format='NETCDF4')

# time = ds.createDimension('time', None)
lat = ds.createDimension('lat', 378)
lon = ds.createDimension('lon', 779)

# times = ds.createVariable('time', 'f4', ('time',))
lats = ds.createVariable('lat', 'f4', ('lat',))
lons = ds.createVariable('lon', 'f4', ('lon',))
value = ds.createVariable('value', 'f4', ('lat', 'lon',))
value.units = 'Unknown'

lats[:] = np.linspace(init_y, end_y, 378)
lons[:] = np.linspace(init_x, end_x, 779)

print('var size before adding data', value.shape)

value[:, :] = data_mask

print('var size after adding first data', value.shape)
# xval = np.linspace(0.5, 5.0, 10)
# yval = np.linspace(0.5, 5.0, 10)
# value[1, :, :] = np.array(xval.reshape(-1, 1) + yval)

# print('var size after adding second data', value.shape)

ds.close()


#%% static data

import netCDF4 as nc
import numpy as np

fn = './data_static.nc'
ds = nc.Dataset(fn, 'w', format='NETCDF4')

lat = ds.createDimension('lat', 3780)
lon = ds.createDimension('lon', 7790)
nsoilstatic = ds.createDimension('nsoilstatic', 10)


lats = ds.createVariable('lat', 'f4', ('lat',))
lons = ds.createVariable('lon', 'f4', ('lon',))
nsoilstatic = ds.createVariable('nsoilstatic', 'f4', ('nsoilstatic',))
value = ds.createVariable('value', 'f4', ('lat', 'lon','nsoilstatic'))
value.units = 'Unknown'

lats[:] = np.linspace(init_y, end_y, 3780)
lons[:] = np.linspace(init_x, end_x, 7790)

print('var size before adding data', value.shape)

value[:, :, :] = data_static

print('var size after adding first data', value.shape)
# xval = np.linspace(0.5, 5.0, 10)
# yval = np.linspace(0.5, 5.0, 10)
# value[1, :, :] = np.array(xval.reshape(-1, 1) + yval)

# print('var size after adding second data', value.shape)

ds.close()


#%% forcing data

import netCDF4 as nc
import numpy as np

fn = './data_forcing.nc'
ds = nc.Dataset(fn, 'w', format='NETCDF4')

lat = ds.createDimension('lat', 378)
lon = ds.createDimension('lon', 779)
time = ds.createDimension('time', 1826)
nforcing = ds.createDimension('nforcing', 2)


lats = ds.createVariable('lat', 'f4', ('lat',))
lons = ds.createVariable('lon', 'f4', ('lon',))
times = ds.createVariable('time', 'f4', ('time',))
nforcing = ds.createVariable('nforcing', 'f4', ('nforcing',))
value = ds.createVariable('value', 'f4', ('lat', 'lon','time','nforcing'))
value.units = 'Unknown'

lats[:] = np.linspace(init_y, end_y, 378)
lons[:] = np.linspace(init_x, end_x, 779)
# time[:] = np.arange(0, 1826, 1)

print('var size before adding data', value.shape)

value[:, :,:,:] = data_forcing

print('var size after adding first data', value.shape)
# xval = np.linspace(0.5, 5.0, 10)
# yval = np.linspace(0.5, 5.0, 10)
# value[1, :, :] = np.array(xval.reshape(-1, 1) + yval)

# print('var size after adding second data', value.shape)

ds.close()


#%% calibration data

import netCDF4 as nc
import numpy as np

fn = './data_cali.nc'
ds = nc.Dataset(fn, 'w', format='NETCDF4')

lat = ds.createDimension('lat', 378)
lon = ds.createDimension('lon', 779)
time = ds.createDimension('time', 335)
# nforcing = ds.createDimension('nforcing', 2)


lats = ds.createVariable('lat', 'f4', ('lat',))
lons = ds.createVariable('lon', 'f4', ('lon',))
times = ds.createVariable('time', 'f4', ('time',))
value = ds.createVariable('value', 'f4', ('lat', 'lon','time'))
value.units = 'Unknown'

lats[:] = np.linspace(init_y, end_y, 378)
lons[:] = np.linspace(init_x, end_x, 779)
# time[:] = np.arange(822,1826,3)

print('var size before adding data', value.shape)

value[:,:,:] = data_cali

print('var size after adding first data', value.shape)
# xval = np.linspace(0.5, 5.0, 10)
# yval = np.linspace(0.5, 5.0, 10)
# value[1, :, :] = np.array(xval.reshape(-1, 1) + yval)

# print('var size after adding second data', value.shape)

ds.close()
